package Eyjafjalla.action;

import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.helper.StringHelper;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.MakeTempCardInHandAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.ArrayList;

public class PinkOceanAction extends AbstractGameAction {
    public PinkOceanAction(int amt,boolean isFire){
        this.amount = amt;
        this.startDuration = duration = Settings.ACTION_DUR_FAST;
        actionType = ActionType.CARD_MANIPULATION;
        this.isFire = isFire;
    }

    boolean isFire;

    private CardGroup tmp = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);

    private static final UIStrings uiStrings;

    private boolean hasSelect = false;

    @Override
    public void update() {
        if(startDuration==duration){
            if(this.amount<=0){
                this.isDone = true;
                return;
            }
            for(int i =0;i<this.amount;i++){
                AbstractCard c = EyjafjallaHelper.getRandomDollyCard(true).makeCopy();
                if(isFire)
                    c.upgrade();
                tmp.group.add(c);
            }
            AbstractDungeon.gridSelectScreen.open(tmp,this.amount,true,uiStrings.TEXT[4]);
        }
        else if(!hasSelect){
            hasSelect = true;
            ArrayList<AbstractCard> cardsToCloud = new ArrayList<>();
            ArrayList<AbstractCard> cardsToHand = new ArrayList<>();
            for(AbstractCard c:tmp.group){
                if(AbstractDungeon.gridSelectScreen.selectedCards.contains(c)){
                    cardsToCloud.add(0,c);
                }
                else {
                    cardsToHand.add(0,c);
                }
            }
            for(AbstractCard c:cardsToCloud){
                addToTop(new MakeTempCardInCloudAction(c));
            }
            for(AbstractCard c:cardsToHand){
                addToTop(new MakeTempCardInHandAction(c));
            }
            AbstractDungeon.gridSelectScreen.selectedCards.clear();
        }


        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
    }
}
